#判断两个单词s1,s2是否变位词
#方法1 逐字检查法
'''
最好情况，时间复杂度是0（1）
最坏情况，时间复杂度是0（n^2）
'''
def angramSolution(s1,s2):
    #预处理，单词长度不同直接结束
    if len(s1)!=len(s2):
        return False
    #1.第二个单词存入列表ls
    ls = list(s2)
    n = len(ls)
    #2.枚举第1个单词的每一个字母
    for c in s1:
        found = False #假设找不到
        #2.1在列表ls中查找c
        for pos in range(n):
            if ls[pos] == c:
                found = True
                ls[pos] = None
                break
        if not found:
            return False
    return True
print("yes"if angramSolution("python","zhejiang") else "no")
#方法2，排序比较
def arangensolution(s1,s2):
    ls1 = list(s1)
    ls2 = list(s2)
    ls1.sort()
    ls2.sort()
    return ls1 == ls2
print("yes"if arangensolution("python","phonyt")else "no")
#方法3暴力法
import itertools as it #导入迭代库
def argramSolution3(s1,s2):
    #1.把单词s1全排列
    tmpList = list(s1)
    ls = list(it.permutations(tmpList,len(tmpList)))
    words = []
    for w in ls:
        word = "".join(w)
        words.append(word)
    #print(words)
    return s2 in words
print("yes"if argramSolution3("zhang","hazng")else "no")
#方法4，计数法
'''
最好情况，时间复杂度是0（n!）
最坏情况，时间复杂度是0（n!）
'''
def argramSolution4(s1,s2):
    #1.构造2个列表记录2个单词每个字母出现的次数。初始化为26个0
    w1=[0]*26
    w2=[0]*26
    #2.遍历单词s1
    for c in s1:
        t =ord(c)-ord('a')
        w1[t]+=1
    #2.遍历单词s2
    for c in s2:
        t=ord(c)-ord('a')
        w2[t]+=1
    return w1==w2
print("yes"if argramSolution4("zhh","hhz")else "no")
